#include "CServer.hpp"
#include <boost/asio/io_context.hpp>
#include <boost/asio/signal_set.hpp>
#include <boost/filesystem.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/property_tree/ini_parser.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/ptree_fwd.hpp>
#include <iostream>
#include <memory>
boost::property_tree::ptree config;

int main() {
  // 读取配置文件
  boost::property_tree::ini_parser::read_ini("./config.ini", config);
  if (config.empty()) {
    std::cerr << "config.ini is empty!" << std::endl;
    return 1;
  }
  try {
    // 事件循环
    net::io_context ioc{1};
    // 信号监听
    boost::asio::signal_set signals(ioc, SIGINT, SIGTERM);
    signals.async_wait([&ioc](boost::system::error_code ec, int signal_number) {
      if (ec) {
        return;
      }
      ioc.stop();
    });
    // 服务器监听端口
    unsigned short port = config.get<unsigned short>("GateServer.Port");
    // 创建服务
    auto server = std::make_shared<CServer>(ioc, port);
    // 运行服务
    server->Start();
    std::cout << "Gate Server listen on port: " << port << std::endl;
    ioc.run();

  } catch (std::exception &e) {
    std::cerr << e.what() << "\n";
  }
  return 0;
}